package com.wanxs.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wanxs.mall.common.constant.Constant;
import com.wanxs.mall.common.service.impl.CrudServiceImpl;
import com.wanxs.mall.common.utils.PageUtils;
import com.wanxs.mall.product.dao.AttrGroupDao;
import com.wanxs.mall.product.dto.AttrGroupDTO;
import com.wanxs.mall.product.entity.AttrAttrgroupRelationEntity;
import com.wanxs.mall.product.entity.AttrEntity;
import com.wanxs.mall.product.entity.AttrGroupEntity;
import com.wanxs.mall.product.service.AttrAttrgroupRelationService;
import com.wanxs.mall.product.service.AttrGroupService;
import com.wanxs.mall.product.service.AttrService;
import com.wanxs.mall.product.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 属性分组
 *
 * @author wanxs 130001934@qq.com
 * @since 1.0.0 2023-09-01
 */
@Service
public class AttrGroupServiceImpl extends CrudServiceImpl<AttrGroupDao, AttrGroupEntity, AttrGroupDTO> implements AttrGroupService {


    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    private AttrService attrService;

    @Autowired
    public AttrGroupServiceImpl(@Lazy AttrService attrService) {
        this.attrService = attrService;
    }


    @Override
    public QueryWrapper<AttrGroupEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }





    /**
     * 查询列表数据
     *    根据列表编号来查询
     * @param params
     * @param catelogId 如何catelogId为0 就不根据catelogId来查询
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        // 获取检索的关键字
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(key)){
            // 拼接查询的条件
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }

        if(catelogId == 0){
            // 不根据categoryId来查询
            IPage<AttrGroupEntity> page =
                    baseDao.selectPage(getPage(params, Constant.CREATE_DATE, false),wrapper);
            return new PageUtils(page);
        }
        // 根据类别编号来查询属性信息
        wrapper.eq("catelog_id",catelogId);
        IPage<AttrGroupEntity> page =
                baseDao.selectPage(getPage(params, null, false),wrapper);
        return new PageUtils(page);
    }

    @Override
    public AttrGroupEntity getEntity(Long id) {
        AttrGroupEntity attrGroupEntity = baseDao.selectById(id);
        return attrGroupEntity;
    }

    @Override
    public void deleteRelation(List<AttrAttrgroupRelationEntity> entityList) {
        attrAttrgroupRelationService.removeBatchRelation(entityList);
    }

    @Override
    public List<AttrGroupEntity> selectList(QueryWrapper<AttrGroupEntity> queryWrapper) {
        return baseDao.selectList(queryWrapper);
    }

    @Override
    public List<AttrGroupWithAttrsVo> withattr(Long catelog_id) {
        List<AttrGroupEntity> attrGroupEntityList = baseDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelog_id));

        List<AttrGroupWithAttrsVo> collect = attrGroupEntityList.stream().map(
                (attrGroupEntity) -> {
                    AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
                    Long attrGroupId = attrGroupEntity.getAttrGroupId();
                    List<AttrEntity> relationAttr = attrService.getRelationAttr(attrGroupId);
                    BeanUtils.copyProperties(attrGroupEntity,vo);
                    vo.setAttrs(relationAttr);
                    return vo;
                }
        ).collect(Collectors.toList());


        return collect;
    }
}